<?php

namespace app\store\controller\ditui;

use app\api\model\Wxapp;
use app\store\controller\Controller;
use app\store\model\Store as StoreModel;
use think\Cache;
use think\Db;

class Dtseller extends Controller
{
    /** 地推专员数据列表 */
    public function index()
    {
        $paramArr = $this->request->param();
        $formInline = isset($paramArr['formInline']) ? $paramArr['formInline'] : "";
        $isAjax = isset($paramArr['isAjax']) ? $paramArr['isAjax'] : 'no';
        $currentPage = isset($paramArr['currentPage']) ? $paramArr['currentPage'] : 1;
        $pageSize = isset($paramArr['pageSize']) ? $paramArr['pageSize'] : 10;
        $limit = " limit ".($currentPage-1)*$pageSize.", ".$pageSize;

        $order = " order by ds_id desc";
        $where = " where ds_shopid=".$this->ad_shopid." ";
        if($formInline!=""){
            trim($formInline['name'])!="" ? $where .= " and ds_truename like '%".$formInline['name']."%'" : "";
            trim($formInline['phone'])!="" ? $where .= " and ds_mobile like '%".$formInline['phone']."%'" : "";
            trim($formInline['remark'])!="" ? $where .= " and ds_remark like '%".$formInline['remark']."%'" : "";
        }
        $sql = "select * from ".getTable('ditui_seller').$where.$order.$limit;
        $list = Db::query($sql);

        //传递总数用于分页
        $sqlNum = "select count(0) as allNum from ".getTable('ditui_seller').$where;
        $allNumArr = Db::query($sqlNum);

        if($isAjax=="yes"){
            $back['list'] = $list;
            $back['allNum'] = $allNumArr[0]['allNum'];
            return $this->renderSuccess('数据请求成功', '', $back);
        }
        //首次非ajax请求，直接控制器推送数据
        else{
            return $this->fetch('ditui/index', ['data' =>['list'=>$list, 'allNum'=>$allNumArr[0]['allNum']]]);
        }
    }

    /** 添加地推人员 */
    public function addSeller()
    {
        $paramArr = $this->request->param();
        $sellerId = isset($paramArr['sellerId']) ? $paramArr['sellerId'] : 0;
        $dailiUser = Db::name('dealer_user')->where('user_id', $sellerId)->find();
        if(!$dailiUser){
            return $this->renderError('ID号未找到或未申请地推员');
        }

        $shopSellerUser = Db::name('ditui_seller')->where(['ds_userid'=>$sellerId, 'ds_shopid'=>$this->ad_shopid])->find();
        if($shopSellerUser){
            return $this->renderError('ID号已经添加过了，无需重复添加');
        }

        $userObj = Db::name('user')->where('user_id', $sellerId)->find();
        $inArr['ds_userid'] = $sellerId;
        $inArr['ds_truename'] = $dailiUser['real_name'];
        $inArr['ds_mobile'] = $userObj['true_mobile'];
        $inArr['ds_nikename'] = $userObj['nickName'];
        $inArr['ds_headerimg'] = $userObj['avatarUrl'];
        $inArr['ds_shopid'] = $this->ad_shopid;
        $inArr['ds_shopname'] = $this->ad_shopname;
        $inArr['wxapp_id'] = $this->wxapp_id;
        $inArr['ds_addtime'] = date('Y-m-d H:i:s',time());
        Db::name('ditui_seller')->insert($inArr);

        $back['state'] = 'addOk';
        $back['msg'] = '添加成功';
        return $this->renderSuccess('添加成功', '', $back);
    }

    /** 添加推广员备注 */
    public function addRemark(){
        $paramArr = $this->request->param();
        $dsId = isset($paramArr['dsId']) ? $paramArr['dsId'] : 0;
        $remarkTxt = isset($paramArr['remarkTxt']) ? $paramArr['remarkTxt'] : "";

        $upArr['ds_remark'] = $remarkTxt;
        Db::name('ditui_seller')->where('ds_id',$dsId)->update($upArr);

        $back['state'] = 'addOk';
        $back['msg'] = '添加成功';
        return $this->renderSuccess('添加成功', '', $back);
    }

    /** 清空微信的access_token */
    public function clearAccessToken(){
        $wxConfig = Wxapp::getWxappCache();
        $appid = $wxConfig['app_id'];
        $cacheKey = $appid . '@access_token';
        //Cache::clear($cacheKey);
        Cache::rm($cacheKey);
        echo "微信小程序access_token清空成功";
    }
}
